//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-2015, Egret Technology Inc.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class LoadingUI extends egret.Sprite {

    public constructor() {
        super();
         this.startLoad();
        this.createView();
         
         this.addChild(this.bg1);
         this.addChild(this.bg2);
         this.addChild(this.bg3);
    }
    
    private bg1:eui.Image = new eui.Image;
    private bg2: eui.Image = new eui.Image;
    private bg3: eui.Image = new eui.Image;
    
    private startLoad():void {
        //创建 URLLoader 对象
        var loader:egret.URLLoader = new egret.URLLoader();
        //设置加载方式为纹理
        loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
        //添加加载完成侦听
        loader.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);
        var url:string = "resource/assets/loading/2.png";
        var request:egret.URLRequest = new egret.URLRequest(url);
        //开始加载
        loader.load(request);
    }
    
    private startLoad2():void {
        //创建 URLLoader 对象
        var loader:egret.URLLoader = new egret.URLLoader();
        //设置加载方式为纹理
        loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
        //添加加载完成侦听
        loader.addEventListener(egret.Event.COMPLETE, this.onLoadComplete2, this);
        var url:string = "resource/assets/loading/3.png";
        var request:egret.URLRequest = new egret.URLRequest(url);
        //开始加载
        loader.load(request);
        
    }
    
    private onLoadComplete2(event: egret.Event): void
    {
        var loader: egret.URLLoader = <egret.URLLoader>event.target;
        //获取加载到的纹理对象
        var texture: egret.Texture = <egret.Texture>loader.data;
        this.bg3.source = texture;
        this.bg3.x = 275;
        this.bg3.y = 450;
    }

    
    private onLoadComplete(event: egret.Event): void
    {
        var loader: egret.URLLoader = <egret.URLLoader>event.target;
        //获取加载到的纹理对象
        var texture: egret.Texture = <egret.Texture>loader.data;
       
        this.quan = this.scale(this, texture, 186, 187, 1000);    
        
        this.startLoad2();
    }
    
    private quan: egret.Sprite;
    
    public scale(parent: egret.Sprite, skin: egret.Texture, w: number, h: number, time: number): egret.Sprite
    {
        var layer: egret.Sprite = new egret.Sprite;  
        parent.addChild(layer);

        var sp: egret.Sprite = new egret.Sprite;
        layer.addChild(sp);  

        var bg2: eui.Image = new eui.Image;
        bg2.source = skin;
        bg2.x = - (w >> 1);
        bg2.y = - (h >> 1);
        sp.x = (w >> 1) + 225;
        sp.y = 480;
        sp.addChild(bg2);
        this.onCompleteHandler(sp);
        this.addChild(sp);
        return layer;
    }
    
    public onCompleteHandler(sp:egret.Sprite):void
    {
        egret.Tween.get(sp).to({
            rotation: 360
        }, 1000).call(this.onCompleteHandler, this, [sp]);
    }
    
    private textField:egret.TextField;

    private createView():void {
        this.textField = new egret.TextField();
        this.addChild(this.textField);
        this.textField.x = 85
        this.textField.y = 580;
        this.textField.width = 480;
        this.textField.height = 100;
        this.textField.textAlign = "center";
    }

    public setProgress(current, total):void {
        
        if(this.textField) this.textField.text = (current / total * 100 >> 0) + "%" ; 
    }
}
